package com.biejiaogroup.librarymanager.controller;

import com.biejiaogroup.librarymanager.entity.Book;
import com.biejiaogroup.librarymanager.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: JesseChan
 * @Date: 2022/5/5
 */
@WebServlet("/borrowBook")
public class BorrowServlet extends HttpServlet
{
    private BookService borrowService = new BookService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("utf-8");
        String bookID = request.getParameter("bookID");
        String userID = request.getParameter("userID");

        Book book = borrowService.queryBookByID(Integer.parseInt(bookID));
        PrintWriter out = response.getWriter();

        if(book == null || book.getBelongTo() != 0)
        {
            out.write("fail");
        }
        else
        {
            boolean res = borrowService.borrowBook(Integer.parseInt(userID), Integer.parseInt(bookID));
            if(res) out.write("success");
            else out.write("fail");
        }

    }
}
